package com.itheima.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.exception.AuthorizationFailException;
import com.itheima.interceptors.LoginInterceptor;
import com.itheima.pojo.*;
import com.itheima.service.ArticleService;
import com.itheima.service.UserService;
import com.itheima.utils.JwtUtil;
import com.itheima.utils.Md5Util;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/list")
    public Result<String> getArticle(@RequestHeader(name = "Authorization") String token,
                                     HttpServletResponse response) throws AuthorizationFailException {
        return Result.success("所有");
    }

    @PostMapping
    public Result<Article> addArticle(@RequestBody Article article) {
        articleService.addArticle(article);
        return Result.success(article);
    }


    // 想封装查询参数实体类的话，必须要加@RequestBody注解，以Json的数据格式封装到请求体里
//    public Result<PageBean<Article>> pageQueryArticle(@RequestBody ArticleQueryParam articleQueryParam) {
//        return null;
//    }
    @GetMapping
    public Result<PageBean<Article>> pageQueryArticle(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String  state)
    {
        PageBean<Article> pb = articleService.list(pageNum, pageSize, categoryId, state);
        return Result.success(pb);
    }

    @GetMapping("/detail")
    public Result<Article> getArticleDetail(Integer id)
    {
        Article article = articleService.getDetail(id);
        return Result.success(article);
    }

    @PutMapping
    public Result<Article> updateArticle(@RequestBody Article article)
    {
        Article art = articleService.updateArticle(article);
        return Result.success(art);
    }

    @DeleteMapping
    public Result<String> deleteArticle(Integer id) {
        articleService.deleteArticle(id);
        return Result.success("删除成功");
    }
}
